﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DevComponents.WinForms;
using RM2012Server.Controller;
using RM2012Server.Model;
using RM2012Server.Business;

namespace RM2012Server.View
{
    public partial class frmUsers : Office2007Form
    {
        #region Fields
        UserCtrl m_UserCtrl = new UserCtrl();
        public static bool isNew = false;
        #endregion

        #region Constructor
        public frmUsers()
        {
            InitializeComponent();
        }
        #endregion

        #region Load
        private void frmUsers_Load(object sender, EventArgs e)
        {
            isNew = false;
            m_UserCtrl.Show(dgvUsers, bindingNavigatorUsers, txtUserId, txtFullName, rbSex, rbMale, rbFemale, txtAddress, txtPhone, txtEmail, txtUserName, txtSalary, txtRemark);
            SaveItem.Enabled = false;
            if (dgvUsers.Rows.Count > 0)
            {
                bool isSex = Convert.ToBoolean(dgvUsers.Rows[0].Cells["colSex"].Value);
                if (isSex == true)
                {
                    rbFemale.Checked = true;
                }
                else
                {
                    rbMale.Checked = true;
                }
            }
        }
        #endregion

        #region Button Click
        private bool IsValid()
        {
            bool isOK = true;
            try
            {
                double salary = Convert.ToDouble(txtSalary.Text);
            }
            catch
            {
                isOK = false;
            }
            if (txtFullName.Text == "" || txtFullName.Text == null)
            {
                isOK = false;
            }
            return isOK;
        }

        private void btnDongY_Click(object sender, EventArgs e)
        {
            bool sex_choise = false;
            if (rbFemale.Checked == true)
            {
                sex_choise = true;
            } 
            else
            {
                sex_choise = false;
            }
            if (IsValid())
            {
                UserInfo info = new UserInfo(txtUserId.Text, txtUserName.Text, Function.getMD5Hash("1234567"), txtFullName.Text, Convert.ToDouble(txtSalary.Text), txtAddress.Text, txtPhone.Text, txtEmail.Text, sex_choise, txtRemark.Text);
                if (isNew == true)
                {
                    m_UserCtrl.Add(info);
                }
                else
                {
                    m_UserCtrl.UpdateNoPass(info);
                }
                m_UserCtrl.Show(dgvUsers, bindingNavigatorUsers, txtUserId, txtFullName, rbSex, rbMale, rbFemale, txtAddress, txtPhone, txtEmail, txtUserName, txtSalary, txtRemark);
                SaveItem.Enabled = false;
                isNew = false;
            }
            else
            {
                MessageBoxEx.Show("Thông tin không hợp lệ !", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnLamLai_Click(object sender, EventArgs e)
        {
            isNew = false;
            m_UserCtrl.Show(dgvUsers, bindingNavigatorUsers, txtUserId, txtFullName, rbSex, rbMale, rbFemale, txtAddress, txtPhone, txtEmail, txtUserName, txtSalary, txtRemark);
            SaveItem.Enabled = false;
        }
        #endregion

        #region BindingNavigator Item Click
        private void AddNewItem_Click(object sender, EventArgs e)
        {
            isNew = true;
            if (dgvUsers.Rows.Count == 0)
            {
                DeleteItem.Enabled = false;
            }
            DataRow m_Row = m_UserCtrl.AddNewRow();
            m_Row["UserId"] = GetID.User(dgvUsers);
            m_Row["FullName"] = "";
            m_Row["Sex"] = false;
            m_Row["Address"] = "";
            m_Row["Phone"] = "";
            m_Row["Email"] = "";
            m_Row["Salary"] = 0.0;
            m_Row["UserName"] = "";
            m_Row["PassWord"] = "";
            m_Row["Remark"] = "";
            m_UserCtrl.AddNewUser(m_Row);
            bindingNavigatorUsers.BindingSource.MoveLast();
            SaveItem.Enabled = true;
        }

        private void RefreshItem_Click(object sender, EventArgs e)
        {
            isNew = false;
            m_UserCtrl.Show(dgvUsers, bindingNavigatorUsers, txtUserId, txtFullName, rbSex, rbMale, rbFemale, txtAddress, txtPhone, txtEmail, txtUserName, txtSalary, txtRemark);
            SaveItem.Enabled = false;
        }

        private void DeleteItem_Click(object sender, EventArgs e)
        {
            isNew = false;
            if (dgvUsers.Rows.Count == 0)
            {
                DeleteItem.Enabled = false;
            }
            else if (dgvUsers.SelectedRows.Count > 0)
            {
                if (MessageBoxEx.Show("Bạn có chắc chắn xóa?", "XÓA", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    foreach (DataGridViewRow row in dgvUsers.SelectedRows)
                    {
                        bindingNavigatorUsers.BindingSource.RemoveAt(row.Index);
                    }
                    SaveItem.Enabled = true;
                }
            }
        }

        private void SaveItem_Click(object sender, EventArgs e)
        {
            isNew = false;
            bindingNavigatorPositionItem.Focus();
            bool check = true;
            foreach (DataGridViewRow row in dgvUsers.Rows)
            {
                if (row.Cells["colUserId"].Value.ToString() == "" || row.Cells["colFullName"].Value.ToString() == "")
                {
                    check = false;
                }
            }
            if (check)
            {
                if (!m_UserCtrl.SaveData())
                {
                    MessageBoxEx.Show("Không thể thực hiện !", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    SaveItem.Enabled = false;
                }
            }
            else
            {
                MessageBoxEx.Show("Dữ liệu không hợp lệ. Mã và họ tên không được để trống !", "HÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void ExitItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region DataGridView Event
        private void dgvGroup_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            bool isFemale = Convert.ToBoolean(dgvUsers.Rows[e.RowIndex].Cells["colSex"].Value.ToString());
            if (isFemale == true)
            {
                rbMale.Checked = false;
                rbFemale.Checked = true;
            }
            else
            {
                rbMale.Checked = true;
                rbFemale.Checked = false;
            }
        }

        private void dgvGroup_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            SaveItem.Enabled = true;
        }

        private void dgvUsers_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            MessageBox.Show("Dữ liệu không hợp lệ !", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
        #endregion

        #region Searching
        private void rbFullName_CheckedChanged(object sender, EventArgs e)
        {
            lblKeyWord.Text = "Họ tên :";
        }

        private void rbAddress_CheckedChanged(object sender, EventArgs e)
        {
            lblKeyWord.Text = "Địa chỉ :";
        }

        private void rbPhone_CheckedChanged(object sender, EventArgs e)
        {
            lblKeyWord.Text = "Điện thoại :";
        }

        private void rbEmail_CheckedChanged(object sender, EventArgs e)
        {
            lblKeyWord.Text = "Thư điện tử :";
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = new DataTable();
                if (rbFullName.Checked == true)
                {
                    dt = m_UserCtrl.GetByFullName(txtKeyWord.Text);
                }
                else if (rbAddress.Checked == true)
                {
                    dt = m_UserCtrl.GetByAddress(txtKeyWord.Text);
                }
                else if (rbPhone.Checked == true)
                {
                    dt = m_UserCtrl.GetByPhone(txtKeyWord.Text);
                }
                else if (rbEmail.Checked == true)
                {
                    dt = m_UserCtrl.GetByEmail(txtKeyWord.Text);
                }
                else
                {
                    dt = m_UserCtrl.GetByFullName(txtKeyWord.Text);
                }
                m_UserCtrl.ShowWithSource(dgvUsers, bindingNavigatorUsers, txtUserId, txtFullName, rbSex, rbMale, rbFemale, txtAddress, txtPhone, txtEmail, txtUserName, txtSalary, txtRemark, dt);
                SaveItem.Enabled = false;
            }
            catch
            {
                MessageBoxEx.Show("Không tìm thấy kết quả !", "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        #endregion

        #region Context Menu Event Click
        private void AddNewItemContextUser_Click(object sender, EventArgs e)
        {
            AddNewItem_Click(sender, e);
        }

        private void RefreshItemContextUser_Click(object sender, EventArgs e)
        {
            RefreshItem_Click(sender, e);
        }

        private void DeleteItemContextUser_Click(object sender, EventArgs e)
        {
            DeleteItem_Click(sender, e);
        }

        private void SaveItemContextUser_Click(object sender, EventArgs e)
        {
            SaveItem_Click(sender, e);
        }

        private void ExitItemContextUser_Click(object sender, EventArgs e)
        {
            ExitItem_Click(sender, e);
        }
        #endregion
    }
}
